﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SPattern;
using SPattern.BasicClass;
using SPattern.AprioriAll;

namespace SequentialPattern
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            /*
            ItemSet IS;
            Sequence seq1 = new Sequence();
            IS = new ItemSet();
            IS.Add(new Item("20"));
            seq1.Add(IS);
            IS = new ItemSet();
            IS.Add(new Item("90"));
            seq1.Add(IS);

            Sequence seq2 = new Sequence();
            IS = new ItemSet();
            IS.Add(new Item("90"));
            seq2.Add(IS);
            IS = new ItemSet();
            IS.Add(new Item("20"));
            // IS.Add(new Item("90"));
            seq2.Add(IS);

            // MessageBox.Show(seq1.IsSubSequence(seq2).ToString());
            MessageBox.Show(
                String.Format("{0}\r\n{1}\r\n{2}\r\n{3}",
                seq1.ToString(),
                seq2.ToString(),
                seq1.IsSubSequence(seq2),
                seq1.GetComparedAfterSequence(seq2).ToString()));
            
            */
            AprioriAll myApriori = (AprioriAll)SequentialPatternFactory.GetAlgorithm(SequentialPatternMethod.AprioriAll);
            myApriori.LoadFile(txtData.Text);
            myApriori.MinSupport = Convert.ToDouble(txtSupport.Text);
            myApriori.EnableSortPhase = true;
            myApriori.EnableMaximalPhase = true;
            StringBuilder sb = new StringBuilder();
            myApriori.Run();
            foreach (CustomerSequence cs in myApriori.CustomersTransactionData)
            {
                foreach (ItemSet IS2 in cs)
                {
                    foreach (Item item in IS2)
                        sb.AppendLine(String.Format("{0}\t{1}\t{2}", cs.CustomerID, IS2.TimeID, item));
                }
            }
            sb.AppendLine("======================");
            foreach (CustomerSequence cs in myApriori.CustomersTransactionData)
            {
                sb.AppendLine(String.Format("{0}\t{1}", cs.CustomerID, cs.ToString()));
            }

            
            sb.AppendLine("======================");

            foreach (Pattern pattern in myApriori.SequentialPatterns)
            {
                sb.AppendLine(String.Format("{0}\t{1}", pattern.ToString(), pattern.Support));
            }

            sb.AppendLine("Total Patterns : " + myApriori.SequentialPatterns.Count);
            textBox1.Text = sb.ToString();
            
            textBox1.SelectionStart = textBox1.Text.Length;
            textBox1.Focus();
            textBox1.ScrollToCaret();
            /*
            SequentialPatternCollection sequentialPatterns;

            // 文字檔測試
            SequentialPatternCollection.SaveTo("pattern_text.txt", myApriori.SequentialPatterns, SequentialPatternSaveFormat.Text);
            // sequentialPatterns = SequentialPatternCollection.ReadFrom("pattern_text.txt", SequentialPatternSaveFormat.Text);
            // SequentialPatternCollection.SaveTo("pattern_text2.txt", myApriori.SequentialPatterns, SequentialPatternSaveFormat.Text);

            // Xml檔測試
            SequentialPatternCollection.SaveTo("pattern_xml.txt", myApriori.SequentialPatterns, SequentialPatternSaveFormat.Xml);
            sequentialPatterns = SequentialPatternCollection.ReadFrom("pattern_xml.txt", SequentialPatternSaveFormat.Xml);
            SequentialPatternCollection.SaveTo("pattern_xml2.txt", myApriori.SequentialPatterns, SequentialPatternSaveFormat.Xml);

            // Binary檔測試
            SequentialPatternCollection.SaveTo("pattern_binary.txt", myApriori.SequentialPatterns, SequentialPatternSaveFormat.Binary);
            sequentialPatterns = SequentialPatternCollection.ReadFrom("pattern_binary.txt", SequentialPatternSaveFormat.Binary);
            SequentialPatternCollection.SaveTo("pattern_binary2.txt", myApriori.SequentialPatterns, SequentialPatternSaveFormat.Binary);
            */
        }
    }
}
